package ar.edu.unq.mongodbexample.tests.persitence;

import java.util.ArrayList;
import java.util.List;

import ar.edu.unq.mongodbexample.homes.CoursesHome;
import ar.edu.unq.mongodbexample.homes.ProfessorHome;
import ar.edu.unq.mongodbexample.homes.StudentHome;
import ar.edu.unq.mongodbexample.homes.SubjectHome;
import ar.edu.unq.mongodbexample.model.Course;
import ar.edu.unq.mongodbexample.model.Professor;
import ar.edu.unq.mongodbexample.model.Student;
import ar.edu.unq.mongodbexample.model.Subject;
import ar.edu.unq.mongodbexample.persistence.MongoDBManager;
import junit.framework.TestCase;

public class SubjectHomeTestCase extends TestCase {

	SubjectHome home;
	List<Subject> subjects;
	
	protected void setUp() throws Exception {
		this.home= new SubjectHome();
		this.subjects=getSubjects();
	}

	protected void tearDown() throws Exception {
		MongoDBManager.getInstance().getDataStore().getCollection(Subject.class).drop();
		MongoDBManager.getInstance().getDataStore().getCollection(Course.class).drop();
		MongoDBManager.getInstance().getDataStore().getCollection(Student.class).drop();
		MongoDBManager.getInstance().getDataStore().getCollection(Professor.class).drop();
	}
	
	public void testSaveAndGetSubjectById(){
		home.save(new Subject("Estrategias de Persistencia"));
		Subject subject=home.findById("Estrategias de Persistencia");
		assertEquals("Estrategias de Persistencia", subject.getName());
	}
	
	public void testSaveAndGetSubjectByIdAndGetReferences(){
		Subject subject=new Subject("Estrategias de Persistencia");
		subject.setCourse(getCourse(1,"CatedraPrueba",getProfessor(12345678,"Tesone","Pablo","pablo.tesone@gmail.com"),getStudents()));
		home.save(subject);
		
		Subject subjectFound=home.findById("Estrategias de Persistencia");
		
		assertEquals("Estrategias de Persistencia", subjectFound.getName());
		assertEquals(4, subjectFound.getCourse().getStudents().size());
		assertEquals("Pablo", subjectFound.getCourse().getProfessor().getName());
		assertEquals("Tesone", subjectFound.getCourse().getProfessor().getLastName());
		assertEquals("pablo.tesone@gmail.com", subjectFound.getCourse().getProfessor().getEmail());
	}
	
	public Professor getProfessor(int dniNumber,String lastName,String name,String email){
		ProfessorHome professorHome=new ProfessorHome();
		Professor professor=new Professor(dniNumber,lastName,name,email);
		professorHome.save(professor);
		return professor;
	}
	
	public Course getCourse(int courseId,String courseName,Professor professor,List<Student> students){
		CoursesHome coursesHome=new CoursesHome();
		Course course=new Course(1,"CatedraPrueba");
		course.setStudents(students);
		course.setProfessor(professor);
		coursesHome.save(course);
		
		return course;
	}

	public List<Student> getStudents(){
		StudentHome studentHome=new StudentHome();
		studentHome.saveAll(createStudents());
		return studentHome.getAll();
	}
	private List<Student> createStudents() {
    	List<Student> studentsList=new ArrayList<Student>();
        studentsList.add(new Student(00000001,19523,"UnApellido1","UnNombre1","unemailmorphia1@mail.com"));
        studentsList.add(new Student(00000002,19524,"UnApellido2","UnNombre2","unemailmorphia2@mail.com"));
        studentsList.add(new Student(00000003,19525,"UnApellido3","UnNombre3","unemailmorphia3@mail.com"));
        studentsList.add(new Student(00000004,19526,"UnApellido4","UnNombre4","unemailmorphia4@mail.com"));
        return studentsList;
    }
	private List<Subject> getSubjects(){
		return null;
	}

}
